﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Lyh.Controls.WindowsForms.Grid
{
    /// <summary>
    /// Description：自定义数据网格显示控件，继承字 System.Windows.Forms.DataGridView 。
    /// Author：lyh
    /// Date：2014-02-24
    /// </summary>
    public partial class LyhDataGridView : DataGridView
    {
        public LyhDataGridView()
        {
            InitializeComponent();
            InitGridView();
        }

        public LyhDataGridView(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            InitGridView();
        }

        private void InitGridView()
        {
            var columnHeadersDefaultCellStyle = new DataGridViewCellStyle
            {
                Alignment = DataGridViewContentAlignment.MiddleLeft,
                BackColor = Color.FromArgb(230, 243, 251),
                Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134),
                ForeColor = SystemColors.WindowText,
                SelectionBackColor = Color.FromArgb(178, 222, 247),
                SelectionForeColor = SystemColors.HighlightText,
                WrapMode = DataGridViewTriState.True,
                Padding = new Padding(3)
            };

            var rowHeadersDefaultCellStyle = new DataGridViewCellStyle
            {
                Alignment = DataGridViewContentAlignment.MiddleLeft,
                BackColor = Color.FromArgb(230, 243, 251),
                Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134),
                ForeColor = SystemColors.WindowText,
                SelectionBackColor = Color.FromArgb(178, 222, 247),
                SelectionForeColor = SystemColors.HighlightText,
                WrapMode = DataGridViewTriState.True,
                Padding = new Padding(3)
            };

            var defaultCellStyle = new DataGridViewCellStyle
            {
                Alignment = DataGridViewContentAlignment.MiddleLeft,
                BackColor = SystemColors.Window,
                Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134),
                ForeColor = SystemColors.ControlText,
                SelectionBackColor = Color.FromArgb(178, 222, 247),
                SelectionForeColor = Color.Black,
                WrapMode = DataGridViewTriState.False,
                Padding = new Padding(3)
            };

            RowTemplate.Height = 23;
            MultiSelect = false;
            RowHeadersVisible = true;
            AllowUserToAddRows = false;
            AllowUserToDeleteRows = false;
            AllowUserToResizeRows = false;
            EnableHeadersVisualStyles = false;
            ReadOnly = true;
            BackgroundColor = Color.White;
            BorderStyle = BorderStyle.None;
            GridColor = Color.FromArgb(119, 199, 245);
            DefaultCellStyle = defaultCellStyle;
            ColumnHeadersDefaultCellStyle = columnHeadersDefaultCellStyle;
            RowHeadersDefaultCellStyle = rowHeadersDefaultCellStyle;
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

            RowPostPaint += LyhDataGridView_RowPostPaint;
        }

        private void LyhDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (RowHeadersVisible)
            {

                var rectangle = new Rectangle(e.RowBounds.Location.X,
                    e.RowBounds.Location.Y,
                    RowHeadersWidth - 4,
                    e.RowBounds.Height);

                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                    RowHeadersDefaultCellStyle.Font,
                    rectangle,
                    RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
        }
    }
}